package com.finace.money.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.MessageSendUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.CashOperationBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.PushReceiver;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/11/28.
 */

public class MoneyOutActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.tv_right_title)
    TextView tvRightTitle;
    @BindView(R.id.tv_account_balance)
    TextView tvAccountBalance;
    @BindView(R.id.et_money_out_money)
    EditText etMoneyOutMoney;
    @BindView(R.id.et_money_out_bank_card)
    EditText etMoneyOutBankCard;
    @BindView(R.id.button_money_out_sure)
    Button buttonMoneyOutSure;

    @Override
    public int setLayout() {
        return R.layout.activity_money_out;
    }

    @Override
    public void init() {
        tvRightTitle.setVisibility(View.VISIBLE);
        tvHeadTitle.setText(UIUtil.getString(R.string.money_out));
        tvRightTitle.setText(UIUtil.getString(R.string.money_out_record));
        rlLeft.setOnClickListener(this);
        tvRightTitle.setOnClickListener(this);
        buttonMoneyOutSure.setOnClickListener(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_CASH_OPERATION);
        LocalBroadcastManager.getInstance(UIUtil.getContext()).registerReceiver(broadcastReceiver, intentFilter);
        unregisterReceiver(broadcastReceiver);
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if(null != intent){
                CashOperationBean cashOperationBean = (CashOperationBean) intent.getSerializableExtra(PushReceiver.ACTION_CASH_OPERATION);
                if(null != cashOperationBean){
                    UIUtil.showToastSafe(cashOperationBean.getTitle());
                }
            }
        }
    };

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.rl_left:
                onBackPressed();
                break;
            case R.id.tv_right_title:
                Intent intent = new Intent(this, MoneyRecordActivity.class);
                intent.putExtra(Constant.MONEY_RECORD, Constant.MONEY_OUT);
                startActivity(intent);
                break;
            case R.id.button_money_out_sure:
                String amount = etMoneyOutMoney.getText().toString();
                if (StringUtil.isNullString(amount)) {
                    UIUtil.showToastSafe(getString(R.string.money_withdraw_input_please));
                    return;
                }
                MessageSendUtil.CashOperationRequest(etMoneyOutMoney.getText().toString(), "WithDraw");
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

}
